<?php
namespace app\controller\admin\http;


class Auth
{
    public function handle($request, \Closure $next){
        $banIPList = getBanIPList();
        if(!empty($banIPList) and is_array($banIPList) and in_array($request->ip(), $banIPList)){
            redirect(url('/admin/miss/ip'))->send();die;
        }

        $token = session('token');
        if(empty($token)){
            return loginFailure();
        }

        $admin = getCaches($token, []);
        if(empty($admin)){
            return loginFailure();
        }

        //默认首页和控制器的主页不需要验证
        if($admin['is_super'] != 1){
            if($request->action() !== 'index' and getController() !== 'index'){
                if(
                    (!isset($admin['auth']) or empty($admin['auth']))
                    or
                    ( !in_array($request->method() . ':' . strtolower(str_replace('.html', '', $request->baseUrl())), $admin['auth']))
                ){
                    redirect(url('/admin/miss/auth'))->send();die;
                }
            }
        }
        $request->admin = $admin;
        //判断地址是否存在
        return $next($request);
    }

}